package com.example.feifei.view;

import android.animation.ArgbEvaluator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;

/**
 * 散花效果实现
 * Created by feifei on 2017/7/20.
 */

public class InterDotsView extends View {

    private int DOTS_COUNT = 6;//画圆数量

    private Paint[] circlePaints = new Paint[4];

    private int centerX;//当前原点的X坐标
    private int centerY;//当前原点的Y坐标

    private float maxInnerDotsRadius;
    private float maxOuterDotsRadius;
    private float maxDotSize;//散花圆半径

    private float currentRadius1 = 0;
    private static final int OUTER_DOTS_POSITION_ANGLE = 60;
    private float currentDotSize1 = 0;

    private float currentProgress = 0;

    private ArgbEvaluator argbEvaluator = new ArgbEvaluator();


    private static final int COLOR_1 = 0xFFFFC107;
    private static final int COLOR_2 = 0xFFFF9800;
    private static final int COLOR_3 = 0xFFFF5722;
    private static final int COLOR_4 = 0xFFF44336;

    public InterDotsView(Context context) {
        super(context);
        initView();
    }

    public InterDotsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public InterDotsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(400,400);
    }

    //初始化视图
    private void initView(){
        for(int i=0;i<circlePaints.length;i++){
            circlePaints[i] = new Paint();
            circlePaints[i].setStyle(Paint.Style.FILL);//实心画笔
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w/2;
        centerY = h/2;
        maxDotSize = 15;
        maxOuterDotsRadius = w/2 - maxDotSize*2;
        maxInnerDotsRadius = 0.8f*maxInnerDotsRadius;
    }
    //开始画圆显示
    @Override
    protected void onDraw(Canvas canvas) {
        drawOutDotFrame(canvas);

    }
    //画圆
    private void drawOutDotFrame(Canvas canvas){
        for(int i=0;i<DOTS_COUNT;i++){
            int cX = (int)(centerX+currentRadius1*Math.cos((i*OUTER_DOTS_POSITION_ANGLE -30)*Math.PI/180));
            int cY = (int)(centerY + currentRadius1*Math.sin((i*OUTER_DOTS_POSITION_ANGLE - 30) *Math.PI/180));
            canvas.drawCircle(cX,cY,currentDotSize1,circlePaints[i%circlePaints.length]);
        }
    }
    //开始尝试位置定位
    public void setCurrentProgress(float currentProgress){
        this.currentProgress = currentProgress;
        updateOuterDotsPosition();
        updateDotsPaints();
        updateDotsAlpha();

        postInvalidate();

    }

    private void updateOuterDotsPosition() {
        if (currentProgress < 0.3f) {
            this.currentRadius1 = (float)mapValueFromRangeToRange(currentProgress, 0.0f, 0.3f, 0, maxOuterDotsRadius * 0.8f);
        } else {
            this.currentRadius1 = (float)mapValueFromRangeToRange(currentProgress, 0.3f, 1f, 0.8f * maxOuterDotsRadius, maxOuterDotsRadius);
        }

        if (currentProgress < 0.9) {
            this.currentDotSize1 = maxDotSize;
        } else {
            this.currentDotSize1 = (float)mapValueFromRangeToRange(currentProgress, 0.7f, 1f, maxDotSize, 0);
        }
    }

    private void updateDotsPaints() {
        if (currentProgress < 0.5f) {
            float progress = (float)mapValueFromRangeToRange(currentProgress, 0f, 0.5f, 0, 1f);
            circlePaints[0].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_1, COLOR_2));
            circlePaints[1].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_2, COLOR_3));
            circlePaints[2].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_3, COLOR_4));
            circlePaints[3].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_4, COLOR_1));
        } else {
            float progress = (float)mapValueFromRangeToRange(currentProgress, 0.5f, 1f, 0, 1f);
            circlePaints[0].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_2, COLOR_3));
            circlePaints[1].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_3, COLOR_4));
            circlePaints[2].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_4, COLOR_1));
            circlePaints[3].setColor((Integer) argbEvaluator.evaluate(progress, COLOR_1, COLOR_2));
        }
    }

    private void updateDotsAlpha() {
        float progress = (float)clamp(currentProgress, 0.6f, 1f);
        int alpha = (int)mapValueFromRangeToRange(progress, 0.6f, 1f, 255, 0);
        circlePaints[0].setAlpha(alpha);
        circlePaints[1].setAlpha(alpha);
        circlePaints[2].setAlpha(alpha);
        circlePaints[3].setAlpha(alpha);
    }

    public static final Property<InterDotsView, Float> DOTS_PROGRESS = new Property<InterDotsView, Float>(Float.class, "dotsProgress") {
        @Override
        public Float get(InterDotsView object) {
            return object.getCurrentProgress();
        }

        @Override
        public void set(InterDotsView object, Float value) {
            object.setCurrentProgress(value);
        }
    };

    public float getCurrentProgress() {
        return currentProgress;
    }


    public double mapValueFromRangeToRange(double value, double fromLow, double fromHigh, double toLow, double toHigh) {
        return toLow + ((value - fromLow) / (fromHigh - fromLow) * (toHigh - toLow));
    }

    public double clamp(double value, double low, double high) {
        return Math.min(Math.max(value, low), high);
    }

}
